package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.Md5Util;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByName(String username) {
        return userMapper.findByName(username);
    }

    @Override
    public void register(String username, String password) {

        //加密
        String util5= Md5Util.getMD5String(password);

         userMapper.register(username,util5);

    }

    @Override
    public void update(User user) {

        //更新用户基本信息
        user.setUpdateTime(LocalDateTime.now());

        userMapper.update(user);


    }

    @Override
    public void updateAvatar(String avatarUrl, Integer id) {
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {

       Map<String,Object> map=ThreadLocalUtil.get();

       Integer id=(Integer) map.get("id");

       String Md5Pwd=Md5Util.getMD5String(newPwd);

        userMapper.updatePwd(Md5Pwd,id);

    }
}
